package com.johnson.news.activity;

import android.os.Bundle;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatus.Builder;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.johnson.news.R;
import com.johnson.news.activity.base.BaseBindingNormalActivity;
import com.johnson.news.databinding.ActivityContactusBinding;
import com.johnson.news.model.bean.ContactItem;
import com.johnson.news.model.remote.RemoteDataSource;
import com.johnson.news.util.Utils;
import com.youmbe.library.util.MyToast;

import java.util.ArrayList;

import io.reactivex.functions.Consumer;

public class ContactusActivity extends BaseBindingNormalActivity<ActivityContactusBinding> {

//    TextView address;
//    TextView tel;
//    TextView zip;
//
    MapView mapview;
    private BaiduMap mBaiduMap;
    float maxZoom;
    float minZoom;

    // 定位相关
    MyLocationData locData = null;
    private Builder mMapStatusBuilder;
    private MapStatus mMapStatus;
    private MapStatusUpdate mMapUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        initView();

        if (!Utils.isNetworkAvailable(this)) {
            MyToast.showToast("暂无网络", this);
            return;
        }

        getData();
    }

    protected void initView() {
//
//        address = (TextView) findViewById(R.id.address);
//        tel = (TextView) findViewById(R.id.tel);
//        zip = (TextView) findViewById(R.id.zip);
//
//        mapview = (MapView) findViewById(R.id.bmapView);

        mapview = dataBinding.bmapView;
        // 地图初始化
        mBaiduMap = mapview.getMap();
        maxZoom = mBaiduMap.getMaxZoomLevel();
        minZoom = mBaiduMap.getMinZoomLevel();
        mapview.showZoomControls(false);

        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);

    }

    @Override
    protected void onPause() {
        mapview.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mapview.onResume();
        super.onResume();
    }

    public void onDestroy() {
        super.onDestroy();
        mapview.onDestroy();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_contactus;
    }

    void getData() {
        RemoteDataSource.getRemoteDataSource()
                .getContactUsItem(this)
                .subscribe(new Consumer<ContactItem>() {
                    @Override
                    public void accept(ContactItem contactItems) throws Exception {
                        dataBinding.address.setText(String.format(getString(R.string.contact_address), contactItems.getAddress()));
                        dataBinding.tel.setText(String.format(getString(R.string.contact_tel), contactItems.getTel()));
                        dataBinding.zip.setText(String.format(getString(R.string.contact_zip), contactItems.getZip()));

                        if (!Utils.isEmptyString(contactItems.getMap())) {
                            // 在地图初始化时先设定一个中心点以显示未定位到时的地图状态。
                            ArrayList<String> gps = Utils.SplitString(contactItems.getMap(), ",");
                            if (!gps.isEmpty()) {

                                LatLng p = new LatLng(Double.parseDouble(gps.get(1)),
                                        Double.parseDouble(gps.get(0)));
                                BitmapDescriptor bitmap = BitmapDescriptorFactory
                                        .fromResource(R.mipmap.pins);
                                OverlayOptions option = new MarkerOptions()
                                        .position(p)
                                        .icon(bitmap);
                                mBaiduMap.addOverlay(option);

                                // 设置定位图层的配置（定位模式，是否允许方向信息，用户自定义定位图标）
                                BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
                                        .fromResource(R.mipmap.pins);
                                MyLocationConfiguration config = new MyLocationConfiguration(
                                        LocationMode.NORMAL, true, mCurrentMarker);
                                mBaiduMap.setMyLocationConfigeration(config);

                                mMapStatusBuilder = new MapStatus.Builder();
                                mMapStatus = mMapStatusBuilder.zoom(17).target(p).build();
                                mMapUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
                                mBaiduMap.animateMapStatus(mMapUpdate, 100);
                            }
                        }
                    }
                });
    }

}
